package com.dayuanit.dy15.ebook.ebookadminbackoffice.config;

import com.dayuanit.dy15.ebook.ebookadminbackoffice.entity.Role;
import com.dayuanit.dy15.ebook.ebookadminbackoffice.entity.User;
import com.dayuanit.dy15.ebook.ebookadminbackoffice.mapper.RoleMapper;
import com.dayuanit.dy15.ebook.ebookadminbackoffice.mapper.UserMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Realm体统用户信息 以及角色和权限
 */
public class EbookRealm extends AuthorizingRealm {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 角色和权限
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String username = (String)principalCollection.getPrimaryPrincipal();

        User user = userMapper.getByName(username);

        // 添加角色
        List<Role> roles = roleMapper.listUserRoles(user.getId());

        //角色名称
        List<String> roleNames = roles.stream().map(role -> role.getName()).collect(Collectors.toList());

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addRoles(roleNames);

        // 添加权限
        Set<String> perms = new HashSet();
        for (Role role : roles) {
            List<String> rolePerms = roleMapper.listPerms(role.getId());
            perms.addAll(rolePerms);
        }

        authorizationInfo.addStringPermissions(perms);

        return authorizationInfo;
    }

    /**
     * 认证 也就是登录 获取用户信息
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String)authenticationToken.getPrincipal();
        String password = new String((char[])authenticationToken.getCredentials());
        System.out.println(">>>>>>>>>" + username + " " + password);

        // 通过username查询用户 并校验密码
        User user = userMapper.getByName(username);
        if (null == user || !user.getPassword().equals(password)) {
            throw new AuthenticationException("用户名或者密码错误");
        }

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, password, getName());
        return authenticationInfo;
    }

    @Override
    public String getName() {
        return "EbookRealm";
    }
}
